using System.ComponentModel;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Enums;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;
using System.Drawing;

namespace VSSDKAssist.Recipes.CustomProject.CreateCustomProject
{
	/// <summary>
    /// Custom page for CreateCustomProject recipe
	/// </summary>
	public partial class CreateCustomProjectCP1 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateCustomProjectCP1"/> class.
		/// </summary>
        public CreateCustomProjectCP1()
		{
		}

		/// <summary>
        /// Initializes a new instance of the <see cref="CreateCustomProjectCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public CreateCustomProjectCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the flavor project.
		/// </summary>
        /// <value>The name of the flavor project.</value>
		[RecipeArgument]
        public string ProjectName
		{
			get { return veProjectName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veProjectName.TextBox.Text = value;
				}
			}
		}

        [RecipeArgument]
        public string ProjectFriendlyName
        {
            get { return veProjectFriendlyName.Value.ToString(); }
            set
            {
                if(value != null)
                {
                    veProjectFriendlyName.Text = value;
                }
            }
        }

        [RecipeArgument]
        public string ProjectDescription
        {
            get { return veProjectDescription.Value.ToString(); }
            set
            {
                if(value != null)
                {
                    veProjectDescription.Text = value;
                }
            }
        }

        [RecipeArgument]
        public string ProjectFileExtension
        {
            get { return (string)veProjectExtension.Value; }
            set
            {
                if(value != null)
                {
                    veProjectExtension.TextBox.Text = value;
                }
            }
        }

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
			get
			{
				errorProvider.SetError(veProjectName, string.Empty);

				if(veProjectName.Value != null)
				{
                    string projectName = veProjectName.Value.ToString();

                    if(!projectName.EndsWith("Project"))
                    {
                        projectName += "Project";
                    }

					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
                        string.Concat(projectName, ".", DictionaryService.GetValue("LanguageExtension").ToString())))
					{
						return false;
					}
				}

				return base.IsDataValid;
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();

					this.veProjectName.BeginInit();
					this.veProjectName.ValueType = typeof(string);
					base.Site.Container.Add(veProjectName);
					this.veProjectName.ConverterInstance = new CSharpIdentifierConverter();
					this.veProjectName.EndInit();

                    this.veProjectFriendlyName.BeginInit();
                    this.veProjectFriendlyName.ValueType = typeof(string);
                    base.Site.Container.Add(veProjectFriendlyName);
                    this.veProjectFriendlyName.EndInit();

                    this.veProjectDescription.BeginInit();
                    this.veProjectDescription.ValueType = typeof(string);
                    base.Site.Container.Add(veProjectDescription);
                    this.veProjectDescription.EndInit();

                    this.veProjectExtension.BeginInit();
                    this.veProjectExtension.ValueType = typeof(string);
                    this.veProjectExtension.ConverterInstance = new FileExtensionConverter();
                    base.Site.Container.Add(veProjectExtension);
                    this.veProjectExtension.EndInit();
				}
			}
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veDestinationFolder.ConverterInstance != null)
			{
				errorProvider.SetError(veDestinationFolder, string.Empty);

				if(veDestinationFolder.Value != null)
				{
					DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("DestinationFolder", null);
					errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
			}
		}

        private void veProjectName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            if(veProjectName.ConverterInstance != null)
            {
                errorProvider.SetError(veProjectName, string.Empty);

                if(veProjectName.Value != null)
                {
                    string projectName = veProjectName.Value.ToString();

                    if(!projectName.EndsWith("Project"))
                    {
                        projectName += "Project";
                    }

                    if(CustomPageHelper.ProjectItemExists(
                        DictionaryService,
                        "VSPackageProject",
                        "DestinationFolder",
                        string.Concat(projectName, ".", DictionaryService.GetValue("LanguageExtension").ToString())))
                    {
                        DictionaryService.SetValue("ProjectName", null);
                        errorProvider.SetError(veProjectName, Properties.Resources.InvalidValue);
                    }
                    else
                    {
                        DictionaryService.SetValue("ProjectName", projectName);
                    }
                }
                else
                {
                    DictionaryService.SetValue("ProjectName", null);
                    errorProvider.SetError(veProjectName, Properties.Resources.InvalidValue);
                }
            }
            else
            {
                DictionaryService.SetValue("ProjectName", veProjectName.Value.ToString());
            }
        }

        private void veProjectFriendlyName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            string friendlyName = (string)veProjectFriendlyName.Value;
            DictionaryService.SetValue("ProjectFriendlyName", friendlyName);
        }

        private void veProjectDescription_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            string description = (string)veProjectDescription.Value;
            DictionaryService.SetValue("ProjectDescription", description);
        }

        private void veProjectExtension_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            if(veProjectExtension.ConverterInstance != null)
            {
                errorProvider.SetError(veProjectExtension, string.Empty);

                if(veProjectExtension.Value != null)
                {
                    string projectExtension = veProjectExtension.Value as string;
                    projectExtension = projectExtension.Replace(".","");
                    DictionaryService.SetValue("ProjectFileExtension", projectExtension);
                }
                else
                {
                    DictionaryService.SetValue("ProjectFileExtension", null);
                    errorProvider.SetError(veProjectExtension, Properties.Resources.InvalidValue);
                }
            }
            else
            {
                DictionaryService.SetValue("ProjectFileExtension", veProjectExtension.Value.ToString());
            }
        }

        private void pctFriendlyName_Click(object sender, System.EventArgs e)
        {
            Bitmap bitmap = CustomPageHelper.GetBitmapResource("Project");

            if(bitmap != null)
            {
                InfoForm infoForm = new InfoForm(bitmap);

                infoForm.ShowDialog();
            }
        }

        private void pctDescription_Click(object sender, System.EventArgs e)
        {
            Bitmap bitmap = CustomPageHelper.GetBitmapResource("Project");

            if(bitmap != null)
            {
                InfoForm infoForm = new InfoForm(bitmap);

                infoForm.ShowDialog();
            }
        }
	}
}